from locust import HttpLocust, TaskSet, task, between
from random import randint
import json
import os
# 任务类继承TaskSet
class UserBehaviour(TaskSet):
    # def on_start(self):
    #     """ on_start is called when a Locust start before any task is scheduled """
        # self.login()
    # 抽奖商品列表
    def draw_list(self):
        response = self.client.get('/api/luck_draw/live/list?uniacid=2')
        new_text = json.loads(response.text)
        print(new_text['data']['activity'][0]['id'])
        return new_text['data']['activity'][0]['id']
    # 点击抽奖
    @task
    def draw(self):
        activity_id = self.draw_list()
        user_id = self.random_userid()
        response = self.client.get('/api/luck_draw/live/draw?activity_id=%d&user_id=%d'%(activity_id, user_id), catch_response=True)
        print(response.status_code)
        # 断言
        if response.status_code == 200:
            response.success()
        else:
            response.failure()
    # 随机一个user_id
    def random_userid(self):
        user_id = randint(0, 10000)
        # print(user_id)
        return user_id
class WebsiteUser(HttpLocust):
    task_set = UserBehaviour
    wait_time = between(1, 2)
    host = 'http://yace.969009.com'
if __name__=="__main__":
    #开启master模式
    os.system('locust -f test_index.py --master')
    # os.system('locust -f test_index.py')